package cn.fl.settlement.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.settle.condition.PrjStlTripleRcdQC;
import cn.fl.settlement.service.ISettlementService;
import cn.fl.settlement.vo.PrjStlTripleRcdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 资管结清-三方结清列表数据
 * @Author: liurong
 * @CreateDate: 2020/12/18 14:47
 * @Version: 1.0
 */
@Controller
@RequestMapping("/settle/triple")
public class SettleTripleController extends BaseController {

    @Autowired
    private ISettlementService settlementService;

    /**
     * 到三方结清列表页面
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("settlement/settleTripleList");
        modelAndView.addObject("applyId", request.getParameter("applyId"));
        return modelAndView;
    }

    /**
     * 初始化列表
     *
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initList(HttpServletRequest request) {
        try {
            String currentPage = request.getParameter("page");
            String pageSize = request.getParameter("pagesize");
            String applyIdStr = request.getParameter("applyId");
            if (StringUtils.isEmpty(applyIdStr)) {
                throw new BaseException("结清申请ID不能为空！");
            }
            PrjStlTripleRcdQC condition = new PrjStlTripleRcdQC();
            condition.setApplyId(Long.parseLong(applyIdStr));
            // 按创建时间倒序
            condition.setSortName("CRT_TM");
            condition.setSortOrder("DESC");

            Pager page = new Pager();
            if (StringUtils.isNotEmpty(currentPage)) {
                page.setCurrentPage(Integer.parseInt(currentPage));
            }
            if (StringUtils.isNotEmpty(pageSize)) {
                page.setEveryPage(Integer.parseInt(pageSize));
            }
            page.setCondition(condition);
            Pager pager = settlementService.searchStlTripleListPage(page);
            ListPageVO<PrjStlTripleRcdVO> listPage = new ListPageVO<>((List<PrjStlTripleRcdVO>) pager.getResultList(), pager.getTotalCount());
            return listPage;
        } catch (Exception e) {
            BaseLogUtils.newLogger("加载三方结清列表数据失败！userId=" + CurrentThreadContext.getCurrentUserId() + ",userName=" + CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

}

